package vue.noteEleve;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import metier.Eleve;
import metier.Grade;
import metier.Stage;
import vue.commun.JPanJTable;
import ctr.commun.CommunGest;
import ctr.noteEleve.GestAjoutNoteEleve;

public class JPanAjoutNoteEleve extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7603591805797946182L;
	private JComboBox jcb_Stage;
	private JPanJTable panJTable;
	private JPanAjoutNoteEleve jPanAjoutNoteEleve;
	private Stage stage;
	private Boolean bl_visible;
	private JTextField jtf_Note;
	private JComboBox jcf_Matiere;
	private Eleve eleve ;
	private JTextField jtf_Date;

	
	public JPanAjoutNoteEleve(String titre,Stage stage) {
		
		this.stage = stage;
		this.bl_visible=false;
		this.eleve = new Eleve(null, null, null, null, null, null, null);
		
		
		if (stage!=null) {
			init(titre);
		}else {
			JOptionPane.showMessageDialog(this, "Aucun stage saisi, ajout de note impossible", "Probleme", JOptionPane.WARNING_MESSAGE);
		}
		
		
	}
	
	public  JPanAjoutNoteEleve(String titre,JPanAjoutNoteEleve jPanAjoutNoteEleve) {
		this.stage = jPanAjoutNoteEleve.getStage();
		this.eleve=jPanAjoutNoteEleve.getEleve();
		this.bl_visible=jPanAjoutNoteEleve.getBl_visible();
		this.jPanAjoutNoteEleve = jPanAjoutNoteEleve;
		jcb_Stage = jPanAjoutNoteEleve.getJcb_Stage();
		init(titre);
	}
	
	private  void init(String titre){
	
		
		// On test si des stages existe, sinon message erreur.
		
			// Panel de saisi
			JPanel panelSaisi = new JPanel();
			panelSaisi.setLayout(new GridLayout(1,1,5,5));
			
		
		
			
			// On initialise qu'a l'implementation
			if (this.jPanAjoutNoteEleve==null) {
				// Recuperation et creation de la comboBox de liste des stages.
				CommunGest communGest = new CommunGest();
				jcb_Stage = new JComboBox(communGest.getListeStage().toArray());
				// je selectionne le premier stage
				this.jcb_Stage.setSelectedItem(stage);
				// en cas de changment de ma comboBox je selectionnne un nouveau stage
				this.jcb_Stage.addItemListener(new GestAjoutNoteEleve(this,null));
			}
			
			
			panelSaisi.add(jcb_Stage);
			
			// Panel de label
			JPanel panelLabel = new JPanel();
			panelLabel.setLayout(new GridLayout(1,1,5,5));
			
			panelLabel.add(new JLabel("sélection du stage : "));
			
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
			//Panel de tableau JVector
			JPanel panelTableau = new JPanel();
			if (panJTable == null ){
				//nom des colonnes
				Vector<Object> nomColonne= new Vector<Object>(Arrays.asList(new String[] {"nom","Prenom", "date de naissance"}));
				
				//valeurs des lignes
				Vector<Vector<Object>> dataVector= new Vector<Vector<Object>>();
				
				 //Ajout d'un vector eleve.
				ArrayList<Eleve>lEleve = new CommunGest().getListeEleve(stage);
				for (Eleve eleve : lEleve) {
					dataVector.add(new Vector<Object>(Arrays.asList(new Object[]{
						eleve.getNomEleve(),eleve.getPrenom(),simpleDateFormat.format(eleve.getDateNaissance())	
					})));
				}
				
				panJTable = new JPanJTable(null, dataVector, nomColonne, null);
				//ajout d'un gestionnnaire de souris, lorsque je clique sur le nom d'un eleve il apparait 
				//les informations pour ajouter une nouvelle note à celui-ci
				
				this.panJTable.getJtable().addMouseListener(new GestAjoutNoteEleve(this,this.panJTable));
			}
			panelTableau.add(panJTable);
			
	
			
			// Panel global
			// Avec mise en forme.
			this.setBorder(new TitledBorder(titre));
			
			JPanel panelLabelEleve = new JPanel(new GridLayout(1,1,5,5));
			
			String abrevationGrade="";
			if (this.eleve.getIdGrade()!=null) {
				Grade grade = new CommunGest().getGrade(this.eleve.getIdGrade());
				 abrevationGrade = grade.getAbrev();
			}
			
			
			JLabel lbl_EleveSelectionne = new JLabel("l'éleve selectionné est le "+abrevationGrade+" "
			+this.eleve.getNomEleve()+" "+this.eleve.getPrenom());
			
			panelLabelEleve.add(lbl_EleveSelectionne);
			
			lbl_EleveSelectionne.setEnabled(this.getBl_visible());
			
			JPanel panelAjoutNote = new JPanel(new GridLayout(1,4));
			
			
			
			JLabel lbl_Note = new JLabel("Note");
			JLabel lbl_Matiere = new JLabel("Matiere");
			JLabel lbl_Date = new JLabel("Date");
			
			this.jtf_Note = new JTextField();
			this.jtf_Date = new JTextField();
			
			this.jcf_Matiere = new JComboBox(new CommunGest().getListeMatiere().toArray());
			
			JButton jButtonAjoutEleve = new JButton("Ajouter");
			jButtonAjoutEleve.addActionListener(new GestAjoutNoteEleve(this,null));
			
			
			
			
			//tant que je n'ai pas sélectionné un éleve, mes composants du panel "panelAjoutNote"
			//sont invisibles
			
			lbl_Note.setEnabled(this.getBl_visible());
			lbl_Matiere.setEnabled(this.getBl_visible());
			this.jtf_Note.setEnabled(this.getBl_visible());
			this.jcf_Matiere.setEnabled(this.getBl_visible());
			jButtonAjoutEleve.setEnabled(this.getBl_visible());
			lbl_Date.setEnabled(this.getBl_visible());
			jtf_Date.setText(simpleDateFormat.format(new Date()));
			jtf_Date.setEnabled(this.getBl_visible());
			
			
			//panel pour mon bouton ajouter
			JPanel panelBouton = new JPanel(new GridLayout(1,1));
			panelBouton.add(jButtonAjoutEleve);
			
			
			//j'ajoute mes composants dans mon panel "panelAjoutNote"
			panelAjoutNote.add(lbl_Note);
			panelAjoutNote.add(this.jtf_Note);
			panelAjoutNote.add(lbl_Matiere);
			panelAjoutNote.add(this.jcf_Matiere);
			panelAjoutNote.add(lbl_Date);
			panelAjoutNote.add(this.jtf_Date);
			
			
			
			this.setLayout(new BorderLayout());
			
			//je personnalise mes panels : panneau Nord
			JPanel panneauNord = new JPanel(new GridLayout(1,2,5,5));
			panneauNord.add(panelLabel);
			panneauNord.add(panelSaisi);
			
			//je personnalise mes panels : panneau sud
			JPanel panneauSud = new JPanel(new GridLayout(3,1,5,5));
			panneauSud.add(panelLabelEleve);
			panneauSud.add(panelAjoutNote);
			panneauSud.add(panelBouton);
			
			this.add(panneauNord,BorderLayout.NORTH);
			this.add(panelTableau,BorderLayout.CENTER);
			this.add(panneauSud,BorderLayout.SOUTH);
			
			
		}
		

	public JComboBox getJcb_Stage() {
		return jcb_Stage;
	}

	public JPanJTable getPanJTable() {
		return panJTable;
	}

	public JPanAjoutNoteEleve getjPanAjoutNoteEleve() {
		return jPanAjoutNoteEleve;
	}

	public Stage getStage() {
		return stage;
	}
	public void setStage(Stage stage) {
		this.stage = stage;
	}



	public JTextField getJtf_Note() {
		return jtf_Note;
	}

	public JComboBox getJcf_Matiere() {
		return jcf_Matiere;
	}

	public Boolean getBl_visible() {
		return bl_visible;
	}

	public void setBl_visible(Boolean bl_visible) {
		this.bl_visible = bl_visible;
	}

	public Eleve getEleve() {
		return eleve;
	}

	public void setEleve(Eleve eleve) {
		this.eleve = eleve;
	}

	public JTextField getJtf_Date() {
		return jtf_Date;
	}
	
	
	
	

	
	
	
	
	
	
	


	
	
}
